package me.firstapp.spi.bbs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonAdvertisement;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.bbs.AdvertisementService;

@Service
public class AdvertisementServiceImpl extends BaseServiceImpl implements AdvertisementService {

	@Override
	public ListObject<JsonAdvertisement> findAvailableAds() {
		ListObject<JsonAdvertisement> result = new ListObject<JsonAdvertisement>();
		Map<String, Object> parameters = new HashMap<>();
		JSONObject jsonObject = api(parameters, "/advertisement/findAvailableAds.do", HttpMethod.GET, "查询板块");

		try {
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONArray jsonArray = jsonObject.getJSONArray("items");
				List<JsonAdvertisement> ads = new ArrayList<JsonAdvertisement>();
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject _jsonObject = jsonArray.getJSONObject(i);
						if (_jsonObject != null) {
							JsonAdvertisement ad = JSONObject.toJavaObject(_jsonObject, JsonAdvertisement.class);
							ads.add(ad);
						}
					}
				}
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setItems(ads);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonAdvertisement> addAdvertisement(String title, String description, String image,
			String url, Long startDate, Long endDate, Integer sort) {
		SingleObject<JsonAdvertisement> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("title", title);
			params.put("description", description);
			params.put("image", image);
			params.put("url", url);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("startDate", startDate);
			parameters.put("endDate", endDate);
			parameters.put("sort", sort);

			JSONObject jsonObject = apiBody(parameters, "/advertisement/addAdvertisement.do", "新增广告", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonAdvertisement ad = JSONObject.toJavaObject(jsonObject.getJSONObject("object"),
						JsonAdvertisement.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(ad);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonAdvertisement> updateAdvertisement(Long id, String title, String description,
			String image, String url, Long startDate, Long endDate, Integer status, Integer sort) {
		SingleObject<JsonAdvertisement> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("title", title);
			params.put("description", description);
			params.put("image", image);
			params.put("url", url);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			parameters.put("startDate", startDate);
			parameters.put("endDate", endDate);
			parameters.put("status", status);
			parameters.put("sort", sort);

			JSONObject jsonObject = apiBody(parameters, "/advertisement/updateAdvertisement.do", "更新广告", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonAdvertisement ad = JSONObject.toJavaObject(jsonObject.getJSONObject("object"),
						JsonAdvertisement.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(ad);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonAdvertisement> findAdsPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		PageObject<JsonAdvertisement> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/advertisement/findAdsPage.do", "分页查询广告", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonAdvertisement> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonAdvertisement> adList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonAdvertisement ad = JSONObject.toJavaObject(_jsonObject,
										JsonAdvertisement.class);
								adList.add(ad);
							}
						}
					}
					jsonPage.setList(adList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonAdvertisement> findAdvertisementById(Long id) {
		SingleObject<JsonAdvertisement> result = new SingleObject<JsonAdvertisement>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);

			JSONObject resultJson = api(parameters, "/advertisement/findAdvertisementById.do", HttpMethod.GET,
					"根据id获取广告");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonAdvertisement ad = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonAdvertisement.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(ad);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteAdvertisements(String ids) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("ids", ids);

			JSONObject resultJson = api(parameters, "/advertisement/deleteAdvertisements.do", HttpMethod.POST, "删除广告");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
